package com.century.sjt.EMChat.ui;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;

import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.century.sjt.R;

import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;


public class MapAppPopupWindow extends PopupWindow {

    private Context mContext;

    private String ComeNumber;
    private Button  btn_ok;
    private View mMenuView;
    private LinearLayout mapBaidu;
    private LinearLayout mapGaode;
    private LinearLayout mapTengxun;
    private LinearLayout mapSogou;



    public MapAppPopupWindow(Activity context,double latitude, double longtitude) {
        super(context);
        mContext=context;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.map_app_item, null);

        //设置按钮监听

        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.FILL_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
                mMenuView.setOnTouchListener(new OnTouchListener() {

                    public boolean onTouch(View v, MotionEvent event) {

                        int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                        int y=(int) event.getY();
                        if(event.getAction()==MotionEvent.ACTION_UP){
                            if(y<height){
                                dismiss();
                            }
                        }
                        return true;
            }
        });
        showOnMap(latitude,longtitude);
    }
    private void showOnMap(final double latitude, final double longtitude){
        mapBaidu = (LinearLayout) mMenuView.findViewById(R.id.map_baidu);
        mapGaode = (LinearLayout) mMenuView.findViewById(R.id.map_gaode);
        mapTengxun = (LinearLayout) mMenuView.findViewById(R.id.map_tengxun);
        mapSogou = (LinearLayout) mMenuView.findViewById(R.id.map_sougo);
        if(isAvilible(mContext,"com.baidu.BaiduMap")){//传入指定应用包名
            mapBaidu.setVisibility(View.VISIBLE);
        }
        if (isAvilible(mContext,"com.autonavi.minimap")){
            mapGaode.setVisibility(View.VISIBLE);
        }
//        if (isAvilible(mContext,"com.tencent.map")){
//            mapTengxun.setVisibility(View.VISIBLE);
//        }

        mapBaidu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = null;

                try {
                    intent = Intent.getIntent("intent://map/direction?origin=latlng:"+latitude+","+longtitude+"|" +
                            "title=我的位置&content=目的地&src=yourCompanyName" +
                            "|世纪通#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");
                    mContext.startActivity(intent); //启动调用
                } catch (URISyntaxException e) {
                    Log.e("intent", e.getMessage());
                }
            }
        });
        mapTengxun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        mapSogou.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        mapGaode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    Intent intent1 = new Intent("android.intent.action.VIEW",
                            android.net.Uri.parse("androidamap://navi?sourceApplication=世纪通&poiid=BGVIS1&lat="+latitude+"&lon="+longtitude+"&level=10&dev=0&style=2"));
                    intent1.setPackage("com.autonavi.minimap");
                    mContext.startActivity(intent1);

            }
        });
    }

    /**
     * 检查手机上是否安装了指定的软件
     * @param context
     * @param packageName：应用包名
     * @return
     */
    private boolean isAvilible(Context context, String packageName){
        //获取packagemanager
        final PackageManager packageManager = context.getPackageManager();
        //获取所有已安装程序的包信息
        List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
        //用于存储所有已安装程序的包名
        List<String> packageNames = new ArrayList<String>();
        //从pinfo中将包名字逐一取出，压入pName list中
        if(packageInfos != null){
            for(int i = 0; i < packageInfos.size(); i++){
                String packName = packageInfos.get(i).packageName;
                packageNames.add(packName);
            }
        }
        //判断packageNames中是否有目标程序的包名，有TRUE，没有FALSE
        return packageNames.contains(packageName);
    }

}
